package com.cg.design.graphicdesignpattern.command.drawer;

import com.cg.design.graphicdesignpattern.command.command.MacroCommand;

import java.awt.*;

/**
 * @program: LeetCode->DrawCanvas
 * @description: 实现绘制对象的类，实现了Drawable接口
 * @author: cg
 * @create: 2022-04-05 19:29
 **/
public class DrawCanvas extends Canvas implements Drawable {

    /**
     * 颜色
     */
    private Color color;

    /**
     * 半径
     */
    private int radius;

    /**
     * 历史命令
     */
    private final MacroCommand history;

    /**
     * 构造函数
     *
     * @param width
     * @param height
     * @param history
     */
    public DrawCanvas(int width, int height, MacroCommand history) {
        setSize(width, height);
        setBackground(Color.white);
        this.history = history;
        init();
    }

    @Override
    public void init() {
        color = Color.red;
        radius = 6;
        history.append(new ColorCommand(this, color));
    }

    @Override
    public void paint(Graphics graphics) {
        history.execute();
    }

    @Override
    public void draw(int x, int y) {
        Graphics graphics = getGraphics();
        graphics.setColor(color);
        graphics.fillOval(x - radius, y - radius, radius * 2, radius * 2);
    }

    @Override
    public void setColor(Color color) {
        this.color = color;
    }

}
